package model

import (
	"gin-project/utils"
	"gin-project/utils/errmsg"
	"gorm.io/gorm"
	"mime/multipart"
	"strconv"
)

type SongList struct {
	gorm.Model
	Title        string	`json:"title"`
	Pic          string	`json:"pic"`
	Introduction string	`json:"introduction"`
	Style        string	`json:"style"`
	Songs        []Song `gorm:"many2many:song_list_song;"`
}

func (SongList) TableName() string {
	return "song_list"
}

// 添加歌单
func InsertSongList(songList *SongList) int {
	err := db.Create(&songList).Error
	if err != nil {
		return errmsg.SONGLIST_ADD_FAILED
	}
	return errmsg.SUCCESS
}

// 删除歌单
func DeleteSongList(id string) int {
	convID, _ := strconv.Atoi(id)
	err := db.Where("id = ?", convID).Delete(&SongList{}).Error
	if err != nil {
		return errmsg.SONG_LIST_DELETE_FAILED
	}
	return errmsg.SUCCESS
}

// 根据id查询歌单
func SelectSongListByID(id string) (*SongList, int) {
	convID, _ := strconv.Atoi(id)
	var songList SongList
	err := db.Preload("Songs").Where("id = ?", convID).Find(&songList).Error
	if err != nil {
		return nil, errmsg.SONGLIST_SELECT_FAILED
	}
	return &songList, errmsg.SUCCESS
}

// 查询所有歌单
func SelectSongListAll() ([]SongList, int) {
	var songLists []SongList
	err := db.Find(&songLists).Error
	if err != nil {
		return nil, errmsg.SONGLIST_SELECT_FAILED
	}
	return songLists, errmsg.SUCCESS
}

// 更新歌单
func UpdateSongList(m map[string]interface{}, id string) int {
	convID, _ := strconv.Atoi(id)
	err := db.Model(&SongList{}).Where("id = ?", convID).Updates(m).Error
	if err != nil {
		return errmsg.SONGLIST_UPDATE_FAILED
	}
	return errmsg.SUCCESS
}

// 更新歌单图片
func UpdateSongListPic(file multipart.File, header *multipart.FileHeader) (string, int) {
	url, code := utils.UploadFile(file, header.Size)
	return url, code
}

// 给歌单添加歌曲
func InsertSongToSongList(songListID, songID string) int {
	songList, _ := SelectSongListByID(songListID)
	song, _ := SelectSongByID(songID)
	songList.Songs = append(songList.Songs, song)
	err := db.Save(songList).Error
	if err != nil {
		return errmsg.SONGLIST_ADD_SONG_FAILED
	}
	return errmsg.SUCCESS
}

func DeleteSongListOfSong(songListID, songID string) int {
	songList, _ := SelectSongListByID(songListID)
	_ = DeleteRelateBySongListID(songListID)

	var songs []Song
	for _, v := range songList.Songs {
		idStr := strconv.FormatUint(uint64(v.ID), 10)
		if idStr != songID {
			songs = append(songs, v)
		}
	}

	var code = errmsg.SUCCESS
	for _, v := range songs {
		idStr := strconv.FormatUint(uint64(v.ID), 10)
		code = InsertRelate(songListID, idStr)
	}
	return code
}

func SelectLikeTitle(title string) ([]*SongList, int) {
	var songLists []*SongList
	err := db.Where("title like ?", "%"+title+"%").Find(&songLists).Error
	if err != nil {
		return nil, errmsg.SONGLIST_SELECT_FAILED
	}
	return songLists, errmsg.SUCCESS
}

func SelectSongsBySongListID(id string) (*SongList, int) {
	var songList SongList
	convID, _ := strconv.Atoi(id)
	err := db.Preload("Songs").Where("id = ?", convID).Find(&songList).Error
	if err != nil {
		return nil, errmsg.SONGLIST_SELECT_FAILED
	}
	return &songList, errmsg.SUCCESS
}